// 导入数据库操作
const { sqlOne, sqlBatch } = require('../db/common')
// 加密工具包bcryptjs
const bcrypt = require('bcryptjs')
// 引入jwt
const jwt = require('jsonwebtoken')
// 引入加密字符串
const config = require('../utils/config')
// 导入工具
const utils = require('../utils/index')
const trie = require('../utils/trie');
// 成功响应 onSuccess 失败响应 onError

// 获取文章下的评论
exports.getArticleComment = async (req, res) => {
    const { articleId, pageSize, pageNum, desc } = req.query;
    const startNum = (pageNum - 1) * pageSize;

    let orderBy = 'ASC'; // 默认排序方向为升序  
    if (desc === 'desc') {
        orderBy = 'DESC'; // 如果 desc 为 'desc'，则设置为降序  
    }

    const sql = `  
        SELECT   
            tc.id as commentId,  
            tc.content,  
            tc.createdAt,  
            tu.id as userId,  
            tu.username,  
            tu.avatar   
        FROM   
            t_comment as tc,  
            t_user as tu   
        WHERE   
            tu.id = tc.form_uid   
            AND tc.deletedAt IS NULL   
            AND article_id = ?   
        ORDER BY  
            tc.createdAt ${orderBy}  
        LIMIT   
            ?, ?;  
    `;

    const sqlTotal = `select count(id) as total from t_comment where article_id = ? and deletedAt is NULL;`

    const results = await sqlBatch(sql + sqlTotal, [articleId, startNum, pageSize, articleId]);
    const comment = results[0]
    const total = results[1][0]['total']
    res.onSuccess({ comment, total })
}

// 获取评论我的列表
exports.getCommntMy = async (req, res) => {
    try {
        const { pageSize, pageNum } = req.query;
        const startNum = (pageNum - 1) * pageSize;
        const user_id = req.auth.id
        const sql = `select tu.username,tu.avatar,c.* from t_user as tu,
        (select tc.*,ta.title from t_comment as tc,t_article as ta where  ta.id = tc.article_id and  ta.user_id = ? and tc.deletedAt is NULL ORDER BY tc.createdAt desc limit ?,?) as c 
        where tu.id = c.form_uid`
        const results = await sqlOne(sql, [user_id, startNum, pageSize])
        const sqltotal = `select count(ta.id) as total from t_comment as tc,t_article as ta  where  ta.id = tc.article_id and  ta.user_id = ? and tc.deletedAt is NULL `
        const [{ total }] = await sqlOne(sqltotal, [user_id])
        res.onSuccess({ data: results, total })
    } catch (e) {
        res.onError(e)
    }
}


// 添加评论
exports.addArticleComment = async (req, res) => {
    try {
        const { content, article_id } = req.body;

        // 过滤
        const trieContent = trie.filterSensitiveWords(content)

        const createdAt = new Date();
        const form_id = req.auth.id
        const sql = `insert into t_comment (form_uid,content,article_id,createdAt) values (?,?,?,?)`;
        const results = await sqlOne(sql, [form_id, trieContent, article_id, createdAt])
        res.onSuccess()
    } catch (e) {
        res.onError(e)
    }
}


// 根据id获取
exports.getArticleCommentById = async (req, res) => {
    const { id } = req.params;
    const sql = `  
        SELECT   
            tc.id as commentId,  
            tc.content,  
            tc.createdAt,  
            tu.id as userId,  
            tu.username,  
            tu.avatar   
        FROM   
            t_comment as tc,  
            t_user as tu   
        WHERE   
            tu.id = tc.form_uid   
            AND tc.deletedAt IS NULL   
            AND  tc.id =?
    `;
    const results = await sqlOne(sql, [ id]);
    res.onSuccess(results[0])
}